<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\model\Pivot;
use app\admin\model\User;
use app\admin\model\Role;

class UserRole extends Pivot
{
    protected $table = 'user_roles';
    protected $pk = 'id';
    
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    protected $createTime = 'created_at';
    protected $updateTime = false;
    
    // 字段类型转换
    protected $type = [
        'user_id' => 'integer',
        'role_id' => 'integer',
        'created_at' => 'datetime',
    ];

    /**
     * 用户关联
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * 角色关联
     */
    public function role()
    {
        return $this->belongsTo(Role::class, 'role_id');
    }
}